<template>
    <div>
        <div>
            <el-input
                    style="width: 350px;margin-right: 8px"
                    placeholder="添加职称"
                    prefix-icon="el-icon-plus"
                    v-model="jl.name"
                    @keydown.enter.native="addJobLevel">
            </el-input>
            <el-select style="margin-right: 8px"
                       v-model="jl.titlelevel" placeholder="职称等级">
                <el-option
                        v-for="item in titlelevels"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>
            <el-button icon="el-icon-plus" type="primary" @click="addJobLevel">添加</el-button>
        </div>
        <div style="padding-top: 20px">
            <el-table
                    :data="jobLevels"
                    border
                    stripe
                    style="width: 80%"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职称名称"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="titlelevel"
                        label="职称级别"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="createdate"
                        label="创建时间"
                        width="150"
                >
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="是否启用"
                        width="150"
                >
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" v-if="!scope.row.enabled">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
                title="修改职称"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td><el-tag>职称名</el-tag></td>
                        <td><el-input size="small" v-model="updateJl.name"></el-input></td>
                    </tr>
                    <tr>
                        <td><el-tag>职称级别</el-tag></td>
                        <td><el-select size="small" v-model="updateJl.titlelevel" placeholder="职称等级">
                            <el-option
                                    v-for="item in titlelevels"
                                    :key="item"
                                    :label="item"
                                    :value="item">
                            </el-option>
                        </el-select></td>
                    </tr>
                    <tr>
                        <td><el-tag>是否启用</el-tag></td>
                        <td>
                            <el-switch
                                    size="small"
                                    v-model="updateJl.enabled"
                                    active-text="启用"
                                    inactive-text="禁用">
                            </el-switch>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "JobLevelManager",
        data(){
            return {
                jl:{
                    name:'',
                    titlelevel:''
                },
                updateJl:{
                    name:'',
                    titlelevel:'',
                    enabled:true
                },
                titlelevels:[
                    '正高级',
                    '副高级',
                    '中级',
                    '初级',
                    '员级'
                ],
                jobLevels:[],
                dialogVisible:false,
            }
        },
        methods:{
            initJobLevel(){
                this.getRequest("/system/basic/joblevel/").then(resp=>{
                    if (resp){
                        this.jobLevels = resp;
                    }
                })
            },
            addJobLevel(){
                if (this.jl.name && this.jl.titlelevel){
                    this.postRequest("/system/basic/joblevel/",this.jl).then(resp=>{
                        if (resp){
                            this.initJobLevel();
                            this.jl = {};
                        }
                    })
                } else {
                    this.$message.error('字段不能为空');
                }
            },
            showEditView(index,data){
                //this.updatePos = Object.assign({}, data);//把date放在新的对象里面
                Object.assign(this.updateJl, data);
                this.dialogVisible=true;
            },
            doUpdate(){
                this.putRequest("/system/basic/joblevel/",this.updateJl).then(resp=>{
                    if (resp){
                        this.initJobLevel();
                        this.updateJl.name='';
                    }
                    this.dialogVisible=false;
                })
            },
            handleDelete(index,data){
                this.$confirm('此操作将永久删除【'+data.name+'】职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/joblevel/"+data.id).then(resp=>{
                        if (resp){
                            this.initJobLevel();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
        },
        mounted() {
            this.initJobLevel();
        }
    }
</script>

<style scoped>

</style>